aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/[username]/request/[id]/+page.server.js
blob: 542a3ccb757cd567f5f4e8725f5bc5e15f155f83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { auth, get } from '$lib/utils';
import { redirect } from '@sveltejs/kit';

export let load = async ({ cookies, depends, locals: { user }, params }) => {
	depends('app:invoice');
	if (!user) throw redirect(307, '/login');

	let { request } = await get(`/request/${params.id}`, auth(cookies));
	let { invoice } = request;

	if (invoice) {
		let { amount, received, id } = invoice;
		if (
			request.recipient.username === user.username &&
			((received && !amount) || received >= amount)
		)
			throw redirect(307, `/${user.username}/invoice/${id}/paid`);

		if (request.requester.username === user.username) throw redirect(307, `/send/${id}`);
	}

	return { request };
};